package com.kevin.service.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kevin.common.exception.BusinessException;
import com.kevin.common.json.JsonUtil;
import com.kevin.service.SysParamsCache;
import com.kevin.service.entity.SysParams;
import com.kevin.service.mapper.SysParamsMapper;
import com.kevin.service.service.SysParamsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 参数管理(SysParams)表服务实现类
 *
 * @author makejava
 * @since 2023-03-27 16:13:02
 */
@Service
@RequiredArgsConstructor
public class SysParamsServiceImpl extends ServiceImpl<SysParamsMapper, SysParams> implements SysParamsService {

    private final SysParamsCache sysParamsCache;

    @PostConstruct
    public void init() {
        // 查询列表
        List<SysParams> list = baseMapper.selectList(null);

        // 保存到缓存
        sysParamsCache.saveList(list);
    }

     boolean isExist(String paramKey) {
        return baseMapper.exists(new QueryWrapper<SysParams>().eq("param_key" , paramKey));
    }

    @Override
    public void saveP(SysParams entity) {
        // 判断 参数键 是否存在
        boolean exist = isExist(entity.getParamKey());
        if (exist) {
            throw new BusinessException("参数键已存在");
        }

        baseMapper.insert(entity);

        // 保存到缓存
        sysParamsCache.save(entity.getParamKey(), entity.getParamValue());
    }

    @Override
    public void update(SysParams vo) {
        SysParams entity = baseMapper.selectById(vo.getId());

        // 如果 参数键 修改过
        if (!StrUtil.equalsIgnoreCase(entity.getParamKey(), vo.getParamKey())) {
            // 判断 新参数键 是否存在
            boolean exist = isExist(vo.getParamKey());
            if (exist) {
                throw new BusinessException("参数键已存在");
            }

            // 删除修改前的缓存
            sysParamsCache.del(entity.getParamKey());
        }

        // 修改数据
        updateById(vo);

        // 保存到缓存
        sysParamsCache.save(vo.getParamKey(), vo.getParamValue());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        // 查询列表
        List<SysParams> list = baseMapper.selectBatchIds(idList);

        // 删除数据
        removeByIds(idList);

        // 删除缓存
        String[] keys = list.stream().map(SysParams::getParamKey).toArray(String[]::new);
        sysParamsCache.del(keys);
    }

    @Override
    public String getString(String paramKey) {
        String value = sysParamsCache.get(paramKey);
        if (StrUtil.isBlank(value)) {
            throw new BusinessException("参数不能为空，paramKey：" + paramKey);
        }

        return value;
    }

    @Override
    public int getInt(String paramKey) {
        String value = getString(paramKey);

        return Integer.parseInt(value);
    }

    @Override
    public boolean getBoolean(String paramKey) {
        String value = getString(paramKey);

        return Boolean.parseBoolean(value);
    }

    @Override
    public <T> T getJSONObject(String paramKey, Class<T> valueType) {
        String value = getString(paramKey);

        return JsonUtil.parse(value, valueType);
    }

}

